//
//  OViewController.m
//  OTableViewEmpty
//
//  Created by huizhou.wang on 08/30/2024.
//  Copyright (c) 2024 huizhou.wang. All rights reserved.
//

#import "OViewController.h"
#import <OTableViewEmpty/UITableView+Adapter.h>

@interface OViewController ()<UITableViewDelegate,
UITableViewDataSource,EmptyPlaceholderDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray * dataSource;

@end

@implementation OViewController

- (NSMutableArray *)dataSource {
    if (_dataSource == nil) {
        _dataSource = [NSMutableArray new];
    }
    return  _dataSource;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	
    
    self.tableView = [[UITableView alloc] initWithFrame:
                      CGRectMake(0, 0,
                                 self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor redColor];
    self.tableView.emptyPlaceholderDelegate = self;
    [self.view addSubview:self.tableView];
    
    UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 400)];
    headerView.backgroundColor = [UIColor blueColor];
    self.tableView.tableHeaderView = headerView;
    
    UIView * footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 100)];
    footerView.backgroundColor = [UIColor greenColor];
    self.tableView.tableFooterView = footerView;
    
    [self ttView];
    [self addDataAfter];
}

-(void)ttView {
    
    /**添加按钮*/
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(100, 100, 100, 50);
    btn.backgroundColor = [UIColor redColor];
    [btn addTarget:self action:@selector(addClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    /**删除按钮*/
    UIButton * DelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    DelBtn.frame = CGRectMake(100, 160, 100, 50);
    DelBtn.backgroundColor = [UIColor greenColor];
    [DelBtn addTarget:self action:@selector(delClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:DelBtn];
    
    
}


-(void)addDataAfter{
    [self.dataSource addObject:@"1"];
    [self.dataSource addObject:@"12"];
    [self.dataSource addObject:@"13"];
    [self.dataSource addObject:@"14"];
}

-(void)addClick {
    [self.dataSource addObject:@"电脑"];
    [self.tableView reloadData];
}

-(void)delClick {
    if (self.dataSource.count == 0) return;
    [self.dataSource removeObjectAtIndex:0];
    [self.tableView reloadData];
}

#pragma mark -TABLEVIEW DELEGATE
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

-(UITableViewCell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * ID = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.textLabel.text = self.dataSource[indexPath.row];
    return cell;
}


#pragma MARK - EmptyDataSource
-(UIView *) responsEmptyPlaceholderView:(UIScrollView *)scrollView {
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 400, self.view.bounds.size.width, 500)];
    view.backgroundColor = [UIColor lightGrayColor];
    
    UIButton * refreshButton = [UIButton buttonWithType:UIButtonTypeCustom];
    refreshButton.backgroundColor = [UIColor blueColor];
    refreshButton.frame = CGRectMake(100, 100, 100, 100);
    [refreshButton addTarget:self action:@selector(refreshClick) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:refreshButton];
    return view;
}

-(void)refreshClick {
    [self addClick];
}


@end
